﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Common
{
    /// <summary>
    /// Summary description for Compress
    /// </summary>
    public class Compress
    {
        public Compress()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// 压缩文件，返回字符串
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string CompressObject(string param)
        {
            string compressString = "";
            //声明一个nei
            MemoryStream mstream = new MemoryStream();
            GZipStream cstream = new GZipStream(mstream, CompressionMode.Compress, true);
            StreamWriter bwriter = new StreamWriter(cstream);
            bwriter.Write(param);
            //BinaryWriter bwriter = new BinaryWriter(cstream);
            //bwriter.Write(Encoding.UTF8.GetBytes(param));
            bwriter.Close();
            cstream.Close();
            compressString = Convert.ToBase64String(mstream.ToArray());
            mstream.Close();
            return compressString;
        }
        /// <summary>
        /// 解压缩字符串
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string DepressObject(string param)
        {
            string commonString = "";
            byte[] data = Convert.FromBase64String(param);
            MemoryStream mstream = new MemoryStream(data);
            GZipStream cstream = new GZipStream(mstream, CompressionMode.Decompress);
            StreamReader reader = new StreamReader(cstream);
            commonString = reader.ReadToEnd();
            mstream.Close();
            cstream.Close();
            reader.Close();
            return commonString;
        }
    }
}
